// 当日告警汇总（按业务系统区分，给管理者看，点击更多，可以查看所有业务系统的告警状况）
angular.module('panels')
    .directive("panelBomActiveAlarmToday", function ($http, loadConf, $timeout, $log, moment) {
        return {
            scope: {},
            link: function (scope) {
                function gradientColor(color) {
                    return {
                        radialGradient: {cx: 0.5, cy: 0.3, r: 0.7},
                        stops: [
                            [0, color],
                            [1, Highcharts.Color(color).brighten(-0.1).get('rgb')] // darken
                        ]
                    };
                }

                function getHighChartOptions(store, categories) {
                    return {
                        chart: {
                            type: 'column'
                        },
                        title: {
                            text: ''
                        },
                        xAxis: {
                            categories: categories
                        },
                        yAxis: {
                            min: 0,
                            title: {
                                text: '告警数',
                                enabled: false
                            }
                        },
                        legend: {
                            reversed: true
                        },
                        plotOptions: {
                            series: {
                                stacking: 'normal'
                            }
                        },
                        series: [{// 1:'#DD4444', 2: '#FF7F50', 3: '#0078E7'
                            name: '一般',
                            color: gradientColor('#0078E7'),
                            data: store[3]
                        }, {
                            name: '重要',
                            color: gradientColor('#FF7F50'),
                            data: store[2]
                        }, {
                            name: '严重',
                            color: gradientColor('#DD4444'),
                            data: store[1]
                        }]
                    };
                }

                scope.options = getHighChartOptions({1: [], 2: [], 3: []}, []);
                scope.panelTitle = '今日告警汇总';
                loadConf('panel-bom-active-alarm-today.yml').then(function (setting) {
                    scope.more = setting.more;
                    var preData, timeoutId;
                    scope.$on('$destroy', function () {
                        timeoutId && $timeout.cancel(timeoutId);
                    });
                    (function updateData() {
                        $http.jsonp(setting.countQueryUrl, {params: {date: moment().format('YYYY-MM-DD')}}).success(function (data) {
                            if (angular.equals(data, preData)) {
                                return false;
                            } else {
                                preData = data;
                                data = angular.copy(data);
                            }

                            var store = {1: [], 2: [], 3: []}, categories = [];
                            for (var i = 0; i < data.length; i++) {
                                var bzTypeObj = data[i];
                                categories.push(bzTypeObj["bzTypeName"]);
                                angular.forEach(store, function (seriesData, type) {
                                    seriesData.push(parseInt(bzTypeObj.eventCount[type]) || 0);
                                });
                                // 做多展示10个业务系统
                                if (categories.length == (setting.maxColumnsAllowed || 5)) break;
                            }

                            // 通知更新charts
                            scope.options = getHighChartOptions(store, categories);
                            if (categories.length > 7) {
                                // 如果类型多于7个，横坐标lebel倾斜45度
                                scope.options.xAxis.labels = {
                                    rotation: -45
                                }
                            }
                        }).then(callback, callback);
                        function callback() {
                            if (setting.updateInterval > 0) {
                                timeoutId = $timeout(updateData, setting.updateInterval * 1000);
                            }
                        }

                        $log.debug('panel update data: bom-active-alarm-today');
                    }());
                });
            },
            templateUrl: 'app/panel/highcharts-default.html'
        };
    });